package com.example.blog.modules.system.controller;

import com.example.blog.common.api.ApiResponse;
import com.example.blog.common.security.HasPermission;
import com.example.blog.common.security.SecurityService;
import com.example.blog.modules.system.dto.NotificationDTO;
import com.example.blog.modules.system.service.NotificationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/notifications")
@RequiredArgsConstructor
@Tag(name = "通知管理", description = "系统通知相关接口")
public class NotificationController {

    private final NotificationService notificationService;
    private final SecurityService securityService;

    @Operation(summary = "创建通知")
    @PostMapping
    @HasPermission("notification:create")
    public ApiResponse<NotificationDTO> createNotification(@Valid @RequestBody NotificationDTO notificationDTO) {
        return ApiResponse.success(notificationService.createNotification(notificationDTO));
    }

    @Operation(summary = "删除通知")
    @DeleteMapping("/{id}")
    @HasPermission("notification:delete")
    public ApiResponse<Void> deleteNotification(@PathVariable Long id) {
        notificationService.deleteNotification(id);
        return ApiResponse.success();
    }

    @Operation(summary = "获取通知详情")
    @GetMapping("/{id}")
    @HasPermission("notification:read")
    public ApiResponse<NotificationDTO> getNotification(@PathVariable Long id) {
        return ApiResponse.success(notificationService.getNotification(id));
    }

    @Operation(summary = "获取当前用户的通知列表")
    @GetMapping
    @HasPermission("notification:read")
    public ApiResponse<Page<NotificationDTO>> getCurrentUserNotifications(
            @RequestParam(required = false) Boolean isRead,
            Pageable pageable) {
        Long currentUserId = securityService.getCurrentUserId();
        return ApiResponse.success(notificationService.getUserNotifications(currentUserId, isRead, pageable));
    }

    @Operation(summary = "获取当前用户的未读通知数量")
    @GetMapping("/unread-count")
    @HasPermission("notification:read")
    public ApiResponse<Long> getCurrentUserUnreadCount() {
        Long currentUserId = securityService.getCurrentUserId();
        return ApiResponse.success(notificationService.getUnreadCount(currentUserId));
    }

    @Operation(summary = "标记通知为已读")
    @PostMapping("/{id}/read")
    @HasPermission("notification:update")
    public ApiResponse<Void> markAsRead(@PathVariable Long id) {
        notificationService.markAsRead(id);
        return ApiResponse.success();
    }

    @Operation(summary = "标记所有通知为已读")
    @PostMapping("/mark-all-read")
    @HasPermission("notification:update")
    public ApiResponse<Void> markAllAsRead() {
        Long currentUserId = securityService.getCurrentUserId();
        notificationService.markAllAsRead(currentUserId);
        return ApiResponse.success();
    }

    @Operation(summary = "删除所有已读通知")
    @DeleteMapping("/read")
    @HasPermission("notification:delete")
    public ApiResponse<Void> deleteAllRead() {
        Long currentUserId = securityService.getCurrentUserId();
        notificationService.deleteAllRead(currentUserId);
        return ApiResponse.success();
    }

    @Operation(summary = "发送系统通知")
    @PostMapping("/system")
    @HasPermission("notification:system")
    public ApiResponse<Void> sendSystemNotification(
            @RequestParam String title,
            @RequestParam String content,
            @RequestParam String type) {
        notificationService.sendSystemNotification(title, content, type);
        return ApiResponse.success();
    }

    @Operation(summary = "发送用户通知")
    @PostMapping("/user/{userId}")
    @HasPermission("notification:create")
    public ApiResponse<Void> sendUserNotification(
            @PathVariable Long userId,
            @RequestParam String title,
            @RequestParam String content,
            @RequestParam String type) {
        notificationService.sendUserNotification(userId, title, content, type);
        return ApiResponse.success();
    }
} 